package com.yxinsur.imc.web.controller.business;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yxinsur.imc.business.domain.ImcIns;
import com.yxinsur.imc.business.enums.IsDeletedEnum;
import com.yxinsur.imc.business.form.InsFactorForm;
import com.yxinsur.imc.business.form.InsForm;
import com.yxinsur.imc.business.form.InsSearchForm;
import com.yxinsur.imc.business.service.IImcInsService;
import com.yxinsur.imc.business.vo.InsVo;
import com.yxinsur.imc.business.vo.select.SelectIns;
import com.yxinsur.imc.common.annotation.Log;
import com.yxinsur.imc.common.core.controller.BaseController;
import com.yxinsur.imc.common.core.domain.AjaxResult;
import com.yxinsur.imc.common.core.page.TableDataInfo;
import com.yxinsur.imc.common.enums.BusinessType;
import com.yxinsur.imc.common.utils.SecurityUtils;
import com.yxinsur.imc.common.utils.StringUtils;
import com.yxinsur.imc.common.utils.poi.ExcelUtil;
import com.yxinsur.imc.common.validator.Assert;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 我的保险产品库(险种)Controller
 * 
 * @author imc
 * @date 2021-03-26
 */
@Api(value="我的保险产品库接口服务",tags={"4.2.我的保险产品库接口服务"})
@RestController
@RequestMapping("/business/ins")
public class ImcInsController extends BaseController
{
    @Autowired
    private IImcInsService imcInsService;

    /**
     * 查询我的保险产品库(险种)列表
     */
    @ApiOperation("我的产品分页数据查询")
    @GetMapping("/list")
    public TableDataInfo list(InsSearchForm insForm)
    {
        startPage();
        List<InsVo> list = imcInsService.selectImcInsList(insForm);
        return getDataTable(list);
    }

    /**
     * 导出我的保险产品库(险种)列表
     */
    @ApiOperation("我的产品导出数据查询")
    @Log(title = "我的保险产品库(险种)", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(InsSearchForm insForm)
    {
        List<InsVo> list = imcInsService.selectImcInsList(insForm);
        ExcelUtil<InsVo> util = new ExcelUtil<InsVo>(InsVo.class);
        return util.exportExcel(list, "我的产品库列表数据");
    }

    /**
     * 获取我的保险产品库(险种)详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return AjaxResult.success();
    }

    /**
     * 新增我的保险产品库(险种)
     */
    @ApiOperation("我的产品新增")
    @Log(title = "我的保险产品库(险种)", businessType = BusinessType.INSERT)
    @PostMapping("/{repoInsIds}")
    public AjaxResult add(@PathVariable Long[] repoInsIds)
    {
        return toAjax(imcInsService.insertImcIns(repoInsIds));
    }
    /**
     * 删除我的保险产品库(险种)
     */
    @ApiOperation("我的产品批量删除")
    @Log(title = "我的保险产品库(险种)", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(imcInsService.deleteImcInsByIds(ids));
    }
    /**
     * 修改扩展属性
     */
    @ApiOperation("扩展属性更新")
    @PutMapping("/extend/edit")
    public AjaxResult edit(@RequestBody InsFactorForm factorForm)
    {
        return toAjax(imcInsService.updateImcInsFactors(factorForm));
    }
    /**
     * 扩展属性标题获取
     */
    @ApiOperation("扩展属性标题获取")
    @PostMapping("/extend/{repoInsId}")
    public AjaxResult title(@PathVariable Long repoInsId)
    {
        List<String> titles = new ArrayList<>();
        QueryWrapper<ImcIns> insQueryWrapper = new QueryWrapper<>();
        insQueryWrapper.eq("repo_ins_id",repoInsId);
        insQueryWrapper.eq("is_deleted", IsDeletedEnum.N.getCode());
        insQueryWrapper.eq("tenant_id", SecurityUtils.getTenantId());
        ImcIns ins = this.imcInsService.getOne(insQueryWrapper);
        Assert.isNull(ins,"险种不存在");
        String defaultTitle = "";
        titles.add(StringUtils.isNotBlank(ins.getFactor1Title())?ins.getFactor1Title():defaultTitle);
        titles.add(StringUtils.isNotBlank(ins.getFactor2Title())?ins.getFactor2Title():defaultTitle);
        titles.add(StringUtils.isNotBlank(ins.getFactor3Title())?ins.getFactor3Title():defaultTitle);
        return AjaxResult.success(titles);
    }
    /**
     * 获取险种下拉列表
     */
    @ApiOperation("险种下拉列表数据查询")
    @PostMapping("/select/{repoSupplierId}")
    public List<SelectIns> selectDataList(@PathVariable Long repoSupplierId)
    {
        return this.imcInsService.selectDataList(repoSupplierId);
    }
}
